﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WB.Entity;
using WB.Entity.Helper;
using WB.DaoHelper;
using WB.Library;
using Bussiness.Manager;
using Bussiness.Entity;
public partial class Admin_Product_AddEdit : System.Web.UI.UserControl
{
    
    public Guid Id
    {
        get { return FGuid.ToGuid(hidId.Value); }
    }
    public Guid InfoId
    {
        get { return FGuid.ToGuid(hidInfoId.Value); }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string validGroup = Language + "-edit";
            RequiredFieldValidator1.ValidationGroup = validGroup;
            RequiredFieldValidator2.ValidationGroup = validGroup;
            RequiredFieldValidator3.ValidationGroup = validGroup;
            RequiredFieldValidator4.ValidationGroup = validGroup;
            btnUpdate.ValidationGroup = validGroup;
            LoadCombo();
            hidId.Value = Request["id"];
      
            if(Id != Guid.Empty)
                LoadData();
        }
    }

    protected void Insert()
    {
        //insert vao product
        CmsProductsEntity objPro = new CmsProductsEntity();
        objPro.Id = Guid.NewGuid();
        if (filePhoto.HasFile)
            objPro.Photo = FileUploadControl.FullPath(filePhoto, txtCode.Text, ProductsField.Photo.MaxLength, EnumsFile.Files);
        objPro.Price = double.Parse(txtPrice.Text);
        objPro.ProductCode = txtCode.Text;
        objPro.CategoryId = new Guid(ddlCategory.SelectedValue);
        CmsProductsManager.CreateInstant().Insert(objPro);
        //insert vao product info
        CmsProductInfosEntity objProInfo = new CmsProductInfosEntity();
        objProInfo.Id = Guid.NewGuid();
        objProInfo.ProductId = objPro.Id;
        objProInfo.ProductName = txtProductName.Text;
        objProInfo.Lang = Language;
        objProInfo.Specification = txtContent.Value;
        objProInfo.Title = txtInfor.Text;
        CmsProductInfosManager.CreateInstant().Insert(objProInfo);
        //insert vao ngon ngu con lai
        CmsProductInfosEntity objProInfo2 = new CmsProductInfosEntity();
        objProInfo2.Id = Guid.NewGuid();
        objProInfo2.ProductId = objPro.Id;
        objProInfo2.Lang = Language.ToLower()=="vi-vn"?"en-US":"vi-VN";      
        CmsProductInfosManager.CreateInstant().Insert(objProInfo2);
        Response.Redirect("?control=addpro&id=" + objPro.Id);


    }

    ProductsEntity GetObj()
    {
        return null;
        /*ProductsEntity ob = new ProductsEntity();
        ob.Id = Guid.NewGuid();
        ob.ProductName = txtProductName.Text.Trim();
        ob.ProductCode = txtCode.Text.Trim();
        ob.CategoryId = FGuid.ToGuid(ddlCategory.Text);
        ob.Price = FNumber.ConvertDecimal(txtPrice.Text.Trim());
        ob.Info = txtInfor.Text;
        ob.Contents = txtContent.Value;
        ob.UpdatedBy = Util.CurrentUserName;
        ob.UpdatedDate = DateTime.Now;
        if (filePhoto.HasFile)
            ob.Photo = FileUploadControl.FullPath(filePhoto, ob.ProductName, ProductsField.Photo.MaxLength, EnumsFile.Files);
        return ob;*/
       
    }

    /// <summary>
    /// Loai category : NEWS, PROD
    /// </summary>
    public string Language
    {
        get { return hidLang.Value; }
        set
        {
            hidLang.Value = value;
            //switch (value)
            //{

            //    case "en-US": lblTitle.Text = "CATEGORY DETAIL";
            //        break;
            //    default: lblTitle.Text = "CHI TIẾT CATEGORY";
            //        break;
            //}
        }
    }

    void LoadCombo()
    {
        ddlCategory.DataSource = CmsCategoryManager.CreateInstant().GetCategoryTreeByLang("PROD", Language);
        ddlCategory.DataTextField = "CatName";
        ddlCategory.DataValueField = "Id";
        ddlCategory.DataGroupField = "ParentName";
        ddlCategory.DataBind();
    }

    void LoadData()
    {
        //Response.Write(Id);
        //lay thong tin prodcut
        DataTable tblProduct = CmsProductsManager.CreateInstant().SelectById(Id);
        if (tblProduct.Rows.Count > 0)
        {
            
            txtCode.Text = tblProduct.Rows[0]["ProductCode"].ToString();
            txtPrice.Text = tblProduct.Rows[0]["Price"].ToString();
            ddlCategory.SelectedValue = tblProduct.Rows[0]["Categoryid"].ToString();
            //lay thong tin theo ngon ngu
            CmsProductInfosEntity objCond = new CmsProductInfosEntity();
            objCond.ProductId = Id;
            objCond.Lang = Language;
            if (!string.IsNullOrEmpty(tblProduct.Rows[0]["Photo"].ToString()))
            {
                imgPhoto.Visible = true;
                imgPhoto.ImageUrl = tblProduct.Rows[0]["Photo"].ToString();
            }
            DataTable tblinfo = CmsProductInfosManager.CreateInstant().SelectWithCondition(objCond);
            if (tblinfo.Rows.Count > 0)
            {
                txtProductName.Text = tblinfo.Rows[0]["ProductName"].ToString();
                txtContent.Value = tblinfo.Rows[0]["Specification"].ToString();
                txtInfor.Text = tblinfo.Rows[0]["Title"].ToString();

               
                hidInfoId.Value = tblinfo.Rows[0]["Id"].ToString();
            }

        }

        //ProductsEntity ob = ProductsDao.Instants().SelectOne(Id);
        //if (ob != null)
        //{
        //    txtProductName.Text = ob.ProductName;
        //    txtCode.Text = ob.ProductCode;
        //    ddlCategory.Text = ob.CategoryId.ToString();
        //    txtPrice.Text = FNumber.FormatNumber(ob.Price);
        //    if (!string.IsNullOrEmpty(ob.Photo))
        //    {
        //        imgPhoto.Visible = true;
        //        imgPhoto.ImageUrl = ob.Photo;
        //    }
        //    txtInfor.Text = ob.Info;
        //    txtContent.Value = ob.Contents;
        //}
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (Id == Guid.Empty)
            {
                Insert();
            }
            else
            {
                //insert vao product
                CmsProductsEntity objPro = new CmsProductsEntity();
                objPro.Id =Id;
                if (filePhoto.HasFile)
                    objPro.Photo = FileUploadControl.FullPath(filePhoto, txtCode.Text, ProductsField.Photo.MaxLength, EnumsFile.Files);
                objPro.Price = double.Parse(txtPrice.Text);
                objPro.ProductCode = txtCode.Text;
                objPro.CategoryId = new Guid(ddlCategory.SelectedValue);
                CmsProductsManager.CreateInstant().Update(objPro);
                //insert vao product info
                CmsProductInfosEntity objProInfo = new CmsProductInfosEntity();
                objProInfo.Id = InfoId;               
                objProInfo.ProductName = txtProductName.Text;
                objProInfo.Lang = Language;
                objProInfo.Specification = txtContent.Value;
                objProInfo.Title = txtInfor.Text;
                CmsProductInfosManager.CreateInstant().Update(objProInfo);
                Response.Redirect("?control=addpro&id=" + objPro.Id);

            }
            //ProductsEntity tmp = ProductsDao.Instants().SelectOne(Id);
            //ProductsEntity ob = GetObj();
            //if (tmp == null)
            //{
            //    ob.CreatedBy = Util.CurrentUserName;
            //    ob.CreatedDate = DateTime.Now;
            //    ProductsDao.Instants().Insert(ob);

            //    Response.Redirect("?control=products");
            //}
            //else
            //{
            //    ob.Id = tmp.Id;
            //    if (string.IsNullOrEmpty(ob.Photo))
            //        ob.Photo = tmp.Photo;
            //    ob.CreatedBy = tmp.CreatedBy;
            //    ob.CreatedDate = tmp.CreatedDate;
            //    ProductsDao.Instants().Update(ob);

            //    Response.Redirect(Request.RawUrl);
            //}
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("?control=prolist");
    }
}
